package com.ht.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ht.domain.bo.TbDoctorBO;
import com.ht.domain.po.TbDepartment;
import com.ht.domain.po.TbDoctor;
import com.ht.domain.po.TbHospital;
import com.ht.domain.po.TbLevel;
import com.ht.mapper.TbDepartmentMapper;
import com.ht.mapper.TbDoctorMapper;
import com.ht.mapper.TbHospitalMapper;
import com.ht.mapper.TbLevelMapper;
import com.ht.service.TbDepartmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TbDepartmentServiceImpl implements TbDepartmentService {

    @Autowired
    private TbDepartmentMapper tbDepartmentMapper;

    @Autowired
    private TbDoctorMapper tbDoctorMapper;

    @Autowired
    private TbHospitalMapper tbHospitalMapper;

    @Autowired
    private TbLevelMapper tbLevelMapper;

    @Override
    public List<TbDoctorBO> queryDepartmentBydepartmentName(String departmentName) {
        //根据科室名称查询科室表
        QueryWrapper<TbDepartment> q = new QueryWrapper();
        q.eq("department_name",departmentName);
        TbDepartment tbDepartmentPO = tbDepartmentMapper.selectOne(q);
        //根据科室id找出所有对应的医生
        QueryWrapper<TbDoctor> qq = new QueryWrapper();
        qq.eq("department_id",tbDepartmentPO.getId());
        qq.eq("audit_status","1");
        List<TbDoctor> list = tbDoctorMapper.selectList(qq);
        List<TbDoctorBO> listBO = new ArrayList<>();
        for (TbDoctor po : list) {
            TbDoctorBO bo = new TbDoctorBO();
            bo.setDepartmentName(departmentName);//科室名称
            //根据医院id查询医院名称
            QueryWrapper<TbHospital> hq = new QueryWrapper<>();
            hq.eq("id",po.getHospitalId());
            TbHospital tbHospital = tbHospitalMapper.selectOne(hq);
            bo.setHospitalName(tbHospital.getHospitalName());
            //根据level查询药师的职位
            QueryWrapper<TbLevel> lq = new QueryWrapper<>();
            lq.eq("id",po.getLevel());
            TbLevel tbLevel = tbLevelMapper.selectOne(lq);
            bo.setLevelName(tbLevel.getLevel());

            BeanUtils.copyProperties(po,bo);
            listBO.add(bo);
        }
        return listBO;
    }
}
